<template>
  <div>
    <el-row class="app-toptool" type="flex">
      <el-col :span="16">
        <Search
          size="mini"
          :search-visible="searchVisible"
          :search-data.sync="searchData"
          :search-form="searchForm"
          @refesh_list="searchgo"
        />
      </el-col>
      <el-col :span="8">
        <div class="btn-group" style="margin-bottom:11px;float: right;">
          <div>
            <el-button
              v-if="checkPermission('/tuanfound/delete')"
              type="primary"
              :disabled="multiple"
              size="mini"
              icon="el-icon-delete"
              @click="del(ids)"
            >删除
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :row-class-name="rowClass"
        row-key="id"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        :border="false"
        :stripe="true"
        class="eltable"
        :data="list"
        style="width: 100%"
        @selection-change="selection"
      >
        <el-table-column align="center" type="selection" width="42" />
        <el-table-column align="center" type="" property="sn" label="拼团编号"  width="80" />
        <el-table-column label="拼团商品" align="left" width="280">
          <template v-if="scope.row.TuanGoods" slot-scope="scope">
              <div v-if="scope.row.TuanGoods.goods" class="image__preview">
                <el-image v-if="scope.row.TuanGoods.goods.image" class="table_list_pic" :src="scope.row.TuanGoods.goods.image" :preview-src-list="[scope.row.TuanGoods.goods.image]" />
              </div>
              <div v-if="scope.row.TuanGoods.goods" class="goodstext">
                <div>{{ scope.row.TuanGoods.goods.name }}</div>
              </div>
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          property="nickname"
          label="团长"
          width=""
        />
        <el-table-column
          align="left"
          property="need"
          label="成团人数"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="join"
          label="已参团人数"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="found_time"
          label="开团时间"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="found_end_time"
          label="结速时间"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="status"
          label="状态"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          label="操作"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id">
              <el-button
                v-if="checkPermission('/tuanfound/detail')"
                size="mini"
                type="primary"
                @click="detail(scope.row)"
              ><i
                class="el-icon-edit"
              />详情</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="page_data.total"
        :page.sync="page_data.page"
        :limit.sync="page_data.limit"
        @pagination="index"
      />
    </div>
    <!--详情-->
    <Detail :info="detailInfo" :opentype="opentype" :show.sync="dialog.detailDialogStatus" size="mini" @refesh_list="index" />
  </div>
</template>
<script>
import Search from '@/components/common/Search'
import Pagination from '@/components/Pagination'
import Detail from '@/views/tuanfound/detail.vue'
import {
  confirm,
  param2Obj
} from '@/utils/common'
export default {
  name: 'tuanfound',
  components: {
    Search,
    Pagination,
    Detail
  },
  data() {
    return {
      dialog: {
        detailDialogStatus:false
      },
      ids: [],
      single: true,
      multiple: true,
      list: [],
      opentype: 'add',
      detailInfo:{},
      loading: false,
      ws: {},
      filename: '',
      page_data: {
        limit: 20,
        page: 1,
        total: 20
      },
      searchVisible: true,
      searchForm: [],
      searchData: {}
    }
  },
  mounted() {
    this.index()
  },
  methods: {
    searchgo() {
      this.page_data.page = 1;
      this.index()
    },
    index() {
      const param = {
        limit: this.page_data.limit,
        page: this.page_data.page
      }
      Object.assign(param, this.searchData)
      Object.assign(param, param2Obj(this.$route.fullPath))
      this.loading = true

      this.$api.post('/tuanfound/index', param).then(res => {
        this.list = res.data.data
        this.page_data.total = res.data.total
        this.loading = false
        // console.log(this.page_data);
        if (this.page_data.page == 1) {
          this.searchForm = [
            {
              type: 'Input',
              label: '关键词',
              prop: 'keyword',
              width: '230px'
            }
          ]
        }
      })
    },
    listUpdate(row, field) {
      if (row.id) {
        this.$api.post('/tuanfound/listUpdate', {
          id: row.id,
          [field]: row[field]
        }).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        })
      }
    },
    detail(row) {
      this.opentype = 'detail'
      const id = row.id ? row.id : this.ids.join(',')
      this.$api.post('/tuanfound/getInfo', {
        id: id
      }).then(res => {
        this.dialog.detailDialogStatus = true
        this.detailInfo = res.data
      })
    },
    del(row) {
      confirm({
        content: '确定要操作吗'
      }).then(() => {
        const ids = row.id ? row.id : this.ids.join(',')

        this.$api.post('/tuanfound/delete', {
          id: ids
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.index()
        })
      }).catch(() => {})
    },
    selection(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    rowClass({
      row,
      rowIndex
    }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return 'rowLight'
        }
      }
    }
  }
}
</script>
<style scoped>
  .image__preview {
    float: left;
  }

  .goodstext {
    float: left;
    padding-left: 10px;
    width: 180px;
  }
  .table_list_pic {
    width: 60px;
    height: 60px;
    vertical-align: middle;
  }
</style>
